#!/bin/csh 
#######################################################################
##
## Create GFED3.1 Daily/3-Hourly Fire Emission Data for CAM5 
##
## Requirements: You need to have NCL and CDO installed on your machine
##
##    NCL : http://www.ncl.ucar.edu/ 
##    CDO : https://code.zmaw.de/projects/cdo 
##
## It takes about ten minutes to complete.
##
## Author : Kai Zhang (kai.zhang@pnnl.gov)  
##
## Last date modified: 2014-06-04
##
#######################################################################

set echo 

setenv work /pic/scratch/zhan524/fire/work
setenv data /pic/scratch/zhan524/fire/data
setenv src  /pic/scratch/zhan524/fire/src
setenv daily "T" 
setenv monthly "T" 

mkdir $work 
cd    $work
cp -f ${src}/*.ncl $work


#######################################################################
## Merge monthly mean fire emission flux and daily (3-hourly) emission ratios.
#######################################################################

csh mkmerge

#######################################################################
## Merge files with dates : 20071231 20080101 ... 20081231 20090101
## and remap data to 2x2.5 grid
#######################################################################

csh mkremap

#######################################################################
## Daily emission for BC, OC, and SO2 
#######################################################################

if($daily == "T") then 



foreach yy (2003 2004 2005 2006 2007 2008 2009 2010 2011 )
foreach tp (BC OC SO2) 

setenv yy ${yy} 
setenv tp ${tp} 

ncl create_emission_cam5_daily_base.ncl >& err_dm_base_${tp}_${yy} & 

end # tp 
end # yy

wait 


#######################################################################
## Creat tmp files containing monthly average emissions  
#######################################################################
foreach yy (2003 2004 2005 2006 2007 2008 2009 2010 2011 )
foreach tp (BC OC SO2)

setenv yy ${yy} 
setenv tp ${tp} 

cdo monavg emis_GFED3.1_daily_${tp}_${yy}_2x2.5.nc tmp_GFED3.1_monthly_${tp}_${yy}_2x2.5.nc >& null & 

end # tp 
end # yy

wait


#######################################################################
## Daily average emission files for CAM5  
#######################################################################
foreach yy (2003 2004 2005 2006 2007 2008 2009 2010 2011 )

setenv yy ${yy} 

ncl create_emission_cam5_daily_num_a1_BCOCgfed.ncl >& err_dm_num_a1_BCOCgfed_${yy} &
ncl create_emission_cam5_daily_num_a1_gfed.ncl     >& err_dm_num_a1_BCOCgfed_${yy} &
ncl create_emission_cam5_daily_so4_a1_gfed.ncl     >& err_dm_so4_a1_gfed_${yy} & 
ncl create_emission_cam5_daily_so2_gfed3.ncl       >& err_dm_so2_${yy} & 
end # yy 

wait

echo "Daily average files done!" 



endif # if($daily == "T") then 



#######################################################################
## Monthly average emission files for CAM5  
#######################################################################

if($monthly == "T") then 



foreach yy (2003 2004 2005 2006 2007 2008 2009 2010 2011 )

setenv yy ${yy} 

ncl create_emission_cam5_monthly_BC.ncl  >& err_mm_BC_${yy}  
ncl create_emission_cam5_monthly_OC.ncl  >& err_mm_OC_${yy} 
ncl create_emission_cam5_monthly_so2_cam5.ncl >& err_mm_so2_cam5_${yy}  & 
ncl create_emission_cam5_monthly_so2_gfed.ncl >& err_mm_so2_gfed_${yy}  & 

#######################################################################
## Need BC and OC files 
#######################################################################

ncl create_emission_cam5_monthly_num_a1_BCOCgfed.ncl >& err_mm_num_a1_BCOCgfed_${yy} & 
ncl create_emission_cam5_monthly_num_a1_cam5.ncl     >& err_mm_num_a1_cam5_${yy} & 
ncl create_emission_cam5_monthly_num_a1_gfed.ncl     >& err_mm_num_a1_gfed_${yy} & 
ncl create_emission_cam5_monthly_num_a2_cam5.ncl     >& err_mm_num_a2_cam5_${yy} & 
ncl create_emission_cam5_monthly_so4_a1_cam5.ncl     >& err_mm_so4_a1_cam5_${yy} & 
ncl create_emission_cam5_monthly_so4_a1_gfed.ncl     >& err_mm_so4_a1_gfed_${yy} & 
ncl create_emission_cam5_monthly_so4_a2_cam5.ncl     >& err_mm_so4_a2_cam5_${yy} & 

end

wait  

echo "Monthly average files done!" 



endif # if($monthly == "T") then 



echo " " 
echo " " 
echo "Program ended successfully!" 
echo " " 
echo " " 




